﻿/// <reference path="jquery.js" />
/// <reference path="Utils.js" />
/// <reference path="Verification.js" />
/// <reference path="js.Home.js" />


var shopping_cart_list = new Array();
var item_count = 0;
function AddShoppingCart(item_id)
{
    $("#Shopping-Cart-Home").fadeIn();
    /* 判断是否已添加到购物车列表 */
    if (jQuery.inArray(item_id, shopping_cart_list) != "-1") {
        Dialog(Lang_Selected);
        return false;
    }
    shopping_cart_list.push(item_id);
    
    /* 添加购物车效果 */
    AddItemHtmlToList(item_id);
    /* 已订购的产品数量显示 */
    ItemCount()
    /* 保存到NET存储 */
    Save_SessionState(item_id,1);
    
}

/* 数量有变化时的效果绑定 */
function ItemCount()
{
    var item_count = shopping_cart_list.length;
    $("#Shopping-Cart-Home h3 em").html("(" + item_count + ")");
    if (item_count > 5)
        $("#Shopping-Cart-Home #Item_List").addClass("item_small");
    return item_count;
}

/* HTML 代码传送购物车 */
function AddItemHtmlToList(item_id)
{
    var newhtml = $("#Item_Template").html();
    var new_tile = $("#Product_" + item_id + " span").html();
    var new_img_url = $("#Product_" + item_id + " img").attr("src");
    newhtml = newhtml.format(new_tile, new_img_url, item_id);
    $("#Shopping-Cart-Home #Item_List").append(newhtml); 
}

function ToPay()
{
    if(ItemCount()>0)
    window.location.href = "Product-Shopping-Cart.aspx?lang="+GetVal("lang",1);
}

function Cancel_Select(item_id)
{
    var arr_id = jQuery.inArray(item_id, shopping_cart_list);
    /* 删除数值中的值 */
    if(jQuery.inArray(item_id, shopping_cart_list) != "-1")
        shopping_cart_list.splice(arr_id, 1);
    
    /* 保存到NET存储 */
    Save_SessionState(item_id, 0)
    /* 删除HTML NODE */
    $("div.pay_item_" + item_id).remove();
    ItemCount();
    
}

/* AJAX 前端信息存储后台session ，可以存储和删除 operating_typ 1添加，0删除 */
function Save_SessionState(item_id,operating_typ)
{
    var html_value = $("#Shopping-Cart-Home #Item_List .pay_item_"+item_id).html();
    html_value = "<div class='item pay_item_" + item_id + "'>" + html_value + "<div>";
    Ajax_Save_SessionState(item_id,operating_typ,html_value)
    
}


function Ajax_Save_SessionState(item_id, operating_typ, html_value) {
    jQuery.ajax({
        url: "../ajax/Shopping-Cart-SeveToSessionState.ashx",
        type: "post",
        cache: "false",
        data: {
            "item_id": item_id,
            "shopping_cart_html": html_value,
            "operating_typ": operating_typ /* 1添加 0删除 */
        },
        dataType: "text",
        //success: function (msg) { alert(msg+"\n" +item_id+"\n count:"+ item_count+ "\n" + shopping_cart_list) }
    });
}



/* 计算总额 */
function Price()
{
    var select_count = $("#Product_Shopping_Item_List tbody code").length; /* 元素数量 */
    var i = 0;
    var price = 0;
    while (i < select_count)
    {
        var price_value = parseFloat($("#Product_Shopping_Item_List tbody").find("code").eq(i).html()) * $("#Product_Shopping_Item_List tbody").find("select").eq(i).val();
        price = price + price_value;
        i++;
    }
    price = price.toFixed(2);
    $("#Product_Shopping_Item_List .footer #Price").hide();
    $("#Product_Shopping_Item_List .footer #Price").html(price);
    $("#Product_Shopping_Item_List .footer #Price").fadeIn();
}

function Price_Change()
{
    $("#Product_Shopping_Item_List tbody select").change(function () { Price() });
}

function Del_Shopping_List_Item(item_id)
{
    Save_SessionState(item_id, 0);
    $("#Product_Shopping_Item_List tbody #tr" + item_id).remove();
    Price();
}

function Submit_Pay(bill_inquiry_Id)
{
    var user_name = $("#User_Name").val();
    var user_phone = $("#User_Phone").val();
    var user_address = $("#User_Address").val();
    var agents = $("#Agents").val();
    var verificationImgCode = $("#VerificationImgCode").val();
    var item_lang = GetVal("lang", 1);
    var all_price = $("#Price").html();

    var item_count_list = new Array();
    var item_list_arr = new Array();
    var select_item_count = $("#Product_Shopping_Item_List tbody select").length; /*已选择的产品数量 */
    
    if (select_item_count < 1) {
        Dialog(Lang_Shopping_Cart_Null)
        return false;
    }
    var i = 0;
    while (i < select_item_count)
    {
        var countValue = $("#Product_Shopping_Item_List tbody").find("select").eq(i).val(); /* 单产品的数量 */
        item_count_list.push(countValue);
        i++;
    }


    if(!ValidateReg("User_Phone",ValidatePhone))
        return false;
    if (!ValidateSelect("Agents"))
        return false;
    if (!ValidateLength("VerificationImgCode", 4, 6))
        return false;


    /* 数据提交 */
    jQuery.ajax({
        url: "../ajax/Product_Bill_Add.ashx",
        type: "post",
        cache: "false",
        data: {
            "bill_id": 0,
            "Bill_User_Name": user_name,
            "Bill_User_Phone": user_phone,
            "Bill_User_Address": user_address,
            "Bill_SendShops": agents,
            "Bill_Status": 0,
            "Bill_Product_List": shopping_cart_list,
            "Bill_Inquiry_Id": bill_inquiry_Id,
            "VerificationImgCode": verificationImgCode,
            "Bill_Count_List": item_count_list.toString(),
            "item_lang": item_lang,
            "Bill_Price": all_price
        },
        dataType: "text",
        success: function (msg) { alert(msg + "\n  在线订单业务暂未开通，尊重期待！"); ImgCodeReplace(); }
    });

}

